#include "MainWindow.h"

MainWindow::MainWindow()
    : mpEngine(0)
{
    QWidget *pCentralWidget = new QWidget();
    setCentralWidget(pCentralWidget);

    createActions();
    createMenus();
    createTools();
    createViewers();

    QString message = tr("Ready");
    statusBar()->showMessage(message);

    setWindowTitle(tr("Model Editor"));
    setMinimumSize(800, 600);
    resize(1024, 768);
}

void MainWindow::newProject()
{
    QString message = tr("New Project");
    statusBar()->showMessage(message);
}

void MainWindow::openProject()
{
    QString message = tr("Open Project");
    statusBar()->showMessage(message);
}

void MainWindow::closeProject()
{
    QString message = tr("Close Project");
    statusBar()->showMessage(message);
}

void MainWindow::saveProject()
{
    QString message = tr("Save Project");
    statusBar()->showMessage(message);
    ne::ModelManager *pModelManager = mpEngine->getModelManager();
    ne::ModelPtr pModel = pModelManager->getByName("TestModel");
    if (pModel.notNull())
    {
        pModel->download();
    }
}

void MainWindow::saveAsProject()
{
    QString message = tr("Save As Project");
    statusBar()->showMessage(message);
}

void MainWindow::quitApplication()
{
    close();
}

void MainWindow::editorObjectWidget()
{
    if (mpEditorObjectWidget->isHidden())
    {
        mpEditorObjectWidget->show();
        mpActionEditorObjectWidget->setChecked(true);
    }
    else
    {
        mpEditorObjectWidget->hide();
        mpActionEditorObjectWidget->setChecked(false);
    }
}

void MainWindow::propertyWidget()
{
    if (mpPropertyWidget->isHidden())
    {
        mpPropertyWidget->show();
        mpActionPropertyWidget->setChecked(true);
    }
    else
    {
        mpPropertyWidget->hide();
        mpActionPropertyWidget->setChecked(false);
    }
}

void MainWindow::animationWidget()
{
    if (mpAnimationWidget->isHidden())
    {
        mpAnimationWidget->show();
        mpActionAnimationWidget->setChecked(true);
    }
    else
    {
        mpAnimationWidget->hide();
        mpActionAnimationWidget->setChecked(false);
    }
}

void MainWindow::aboutApplication()
{
    QMessageBox::about(this, tr("About Model Editor"),
        tr("The <b>Model Editor</b> code by <b>Rockie X.Lee</b>"));
}

void MainWindow::playAnimation()
{
    mpGLWidget->createQuad();
    ne::ModelManager *pModelManager = mpEngine->getModelManager();
    ne::ModelPtr pModel = pModelManager->getByName("TestModel");
    mpEditorObjectWidget->setModel(pModel);
}

void MainWindow::pauseAnimation()
{
    mpEditorObjectWidget->updateTreeWidget();
}

void MainWindow::stopAnimation()
{
    mpGLWidget->deleteQuad();
    mpEditorObjectWidget->clearTreeWidget();
}

void MainWindow::loopAnimation()
{
    if (mpActionLoop->isChecked())
    {
        mpActionLoop->setChecked(false);
    }
    else
    {
        mpActionLoop->setChecked(true);
    }
}

void MainWindow::createActions()
{
    // In File Menu
    mpActionNew = new QAction(tr("&New"), this);
    mpActionNew->setShortcuts(QKeySequence::New);
    mpActionNew->setStatusTip(tr("Create a new project"));
    connect(mpActionNew, SIGNAL(triggered()), this, SLOT(newProject()));

    mpActionOpen = new QAction(tr("&Open"), this);
    mpActionOpen->setShortcuts(QKeySequence::Open);
    mpActionOpen->setStatusTip(tr("Open a project"));
    connect(mpActionOpen, SIGNAL(triggered()), this, SLOT(openProject()));

    mpActionClose = new QAction(tr("&Close"), this);
    mpActionClose->setShortcuts(QKeySequence::Close);
    mpActionClose->setStatusTip(tr("Close the project"));
    connect(mpActionClose, SIGNAL(triggered()), this, SLOT(closeProject()));

    mpActionSave = new QAction(tr("&Save"), this);
    mpActionSave->setShortcuts(QKeySequence::Save);
    mpActionSave->setStatusTip(tr("Save the project"));
    connect(mpActionSave, SIGNAL(triggered()), this, SLOT(saveProject()));

    mpActionSaveAs = new QAction(tr("Save &As"), this);
    mpActionSaveAs->setShortcuts(QKeySequence::SaveAs);
    mpActionSaveAs->setStatusTip(tr("Save as the project"));
    connect(mpActionSaveAs, SIGNAL(triggered()), this, SLOT(saveAsProject()));

    mpActionQuit = new QAction(tr("&Quit"), this);
    mpActionQuit->setShortcuts(QKeySequence::Quit);
    mpActionQuit->setStatusTip(tr("Quit the application"));
    connect(mpActionQuit, SIGNAL(triggered()), this, SLOT(quitApplication()));

    // In Edit Menu

    // In View Menu
    mpActionEditorObjectWidget = new QAction(tr("&Object Viewer"), this);
    mpActionEditorObjectWidget->setStatusTip(tr("Object Viewer"));
    mpActionEditorObjectWidget->setCheckable(true);
    connect(mpActionEditorObjectWidget, SIGNAL(triggered()), this, SLOT(editorObjectWidget()));

    mpActionPropertyWidget = new QAction(tr("&Property Editor"), this);
    mpActionPropertyWidget->setStatusTip(tr("Property Editor"));
    mpActionPropertyWidget->setCheckable(true);
    connect(mpActionPropertyWidget, SIGNAL(triggered()), this, SLOT(propertyWidget()));

    mpActionAnimationWidget = new QAction(tr("&Animation Editor"), this);
    mpActionAnimationWidget->setStatusTip(tr("Animation Editor"));
    mpActionAnimationWidget->setCheckable(true);
    connect(mpActionAnimationWidget, SIGNAL(triggered()), this, SLOT(animationWidget()));

    // In Object Menu

    // In About Menu
    mpActionAboutApplication = new QAction(tr("&About Application"), this);
    mpActionAboutApplication->setStatusTip(tr("About the application"));
    connect(mpActionAboutApplication, SIGNAL(triggered()), this, SLOT(aboutApplication()));

    // In Player Tool Bar
    mpActionPlay = new QAction(QIcon(":/images/play.png"), tr("&Play[F5]"), this);
    mpActionPlay->setShortcut(tr("F5"));
    mpActionPlay->setStatusTip(tr("Play the animation"));
    connect(mpActionPlay, SIGNAL(triggered()), this, SLOT(playAnimation()));

    mpActionPause = new QAction(QIcon(":/images/pause.png"), tr("&Pause[F6]"), this);
    mpActionPause->setShortcut(tr("F6"));
    mpActionPause->setStatusTip(tr("Pause the animation"));
    connect(mpActionPause, SIGNAL(triggered()), this, SLOT(pauseAnimation()));

    mpActionStop = new QAction(QIcon(":/images/stop.png"), tr("&Stop[F7]"), this);
    mpActionStop->setShortcut(tr("F7"));
    mpActionStop->setStatusTip(tr("Stop the animation"));
    connect(mpActionStop, SIGNAL(triggered()), this, SLOT(stopAnimation()));

    mpActionLoop = new QAction(QIcon(":/images/loop.png"), tr("&Loop[F8]"), this);
    mpActionLoop->setShortcut(tr("F8"));
    mpActionLoop->setStatusTip(tr("Loop the animation"));
    mpActionLoop->setCheckable(true);
    connect(mpActionLoop, SIGNAL(triggered()), this, SLOT(loopAnimation()));
}

void MainWindow::createMenus()
{
    mpMenuFile = menuBar()->addMenu(tr("&File"));
    mpMenuFile->addAction(mpActionNew);
    mpMenuFile->addAction(mpActionOpen);
    mpMenuFile->addAction(mpActionClose);
    mpMenuFile->addSeparator();
    mpMenuFile->addAction(mpActionSave);
    mpMenuFile->addAction(mpActionSaveAs);
    mpMenuFile->addSeparator();
    mpMenuFile->addAction(mpActionQuit);

    mpMenuEdit = menuBar()->addMenu(tr("&Edit"));

    mpMenuView = menuBar()->addMenu(tr("&View"));
    mpMenuView->addAction(mpActionEditorObjectWidget);
    mpMenuView->addAction(mpActionPropertyWidget);
    mpMenuView->addAction(mpActionAnimationWidget);

    mpMenuObject = menuBar()->addMenu(tr("&Object"));

    mpMenuObject = menuBar()->addMenu(tr("&About"));
    mpMenuObject->addAction(mpActionAboutApplication);
}

void MainWindow::createTools()
{
    mpToolBar = addToolBar(tr("Animation Player"));
    mpToolBar->addAction(mpActionPlay);
    mpToolBar->addAction(mpActionPause);
    mpToolBar->addAction(mpActionStop);
    mpToolBar->addAction(mpActionLoop);
}

void MainWindow::createViewers()
{
    QHBoxLayout *layout = new QHBoxLayout;
    mpGLWidget = new GLWidget;
    layout->addWidget(mpGLWidget);

    centralWidget()->setLayout(layout);

    mpEditorObjectWidget = new EditorObjectWidget(this);
    mpEditorObjectWidget->setAllowedAreas(Qt::LeftDockWidgetArea);
    addDockWidget(Qt::LeftDockWidgetArea, mpEditorObjectWidget);

    mpPropertyWidget = new PropertyWidget(this);
    mpPropertyWidget->setAllowedAreas(Qt::RightDockWidgetArea);
    ModelPropertyEditor *modelEditor = new ModelPropertyEditor(0);
    mpPropertyWidget->addWidget(modelEditor);
    addDockWidget(Qt::RightDockWidgetArea, mpPropertyWidget);

    mpAnimationWidget = new AnimationWidget(this);
    mpAnimationWidget->setAllowedAreas(Qt::BottomDockWidgetArea);
    addDockWidget(Qt::BottomDockWidgetArea, mpAnimationWidget);

    mpEngine = ne::Engine::GetSingletonPtr();
    if (!mpEngine)
    {
        mpEngine = new ne::Engine();
    }
}
